﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimpleReciever
{
    public partial class Dial : UserControl
    {
        private double angle;
        private int HalfWidth = -1;
        private int HalfHeight = -1;

        public double Angle
        {
            get { return angle; }
            set { angle = value; Invalidate(); }
        }

        public bool Radians { get; set; }


        public Dial()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (HalfWidth == -1)
            {
                HalfHeight = (Size.Height / 2);
                HalfWidth = (Size.Width / 2);
            }

            //Console.WriteLine("Paint");
            e.Graphics.DrawEllipse(Pens.Black, 0, 0, Width - 2, Height - 2);
            if (Radians)
            {
                e.Graphics.DrawLine(Pens.Black, HalfWidth, HalfHeight,
                    HalfWidth + (int)(Math.Cos(Angle) * HalfWidth), HalfHeight + (int)(Math.Sin(Angle) * HalfHeight));
            }
            else
            {
                e.Graphics.DrawLine(Pens.Black, HalfWidth, HalfHeight,
                    HalfWidth + (int)(Math.Cos(dtr(Angle)) * HalfWidth), HalfHeight + (int)(Math.Sin(dtr(Angle)) * HalfHeight));
            }
        }
        private double dtr(double deg)
        {
            return Math.PI * deg / 180.0;
        }
    }
}
